package com.example.demo.database.services.mysql;

import com.example.demo.database.services.base.BaseKernel;
import com.example.demo.database.services.base.SqlCompiler;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class MySqlKernel extends BaseKernel {

    public abstract String getUrl();

    public abstract String getUserName();

    public abstract String getPassword();

    @Override
    public void sqlConnectorBuild() {
        //初始化MySql
        MySqlConnector.build(getUrl(), getUserName(), getPassword());
        //判断有没有migrations表
        String tableName = "migrations";
        AtomicBoolean hasMigrationTable = new AtomicBoolean(false);
        List<Map<String, Object>> list = MySqlConnector.getInstance().showTable();
        list.forEach(stringObjectMap -> {
            stringObjectMap.forEach((key, value) -> {
                if (value.equals(tableName)) {
                    hasMigrationTable.set(true);
                }
            });
        });
        //如果有表则不管
        if (hasMigrationTable.get()) {

        } else {
            //如果没有则创建这个表
            MySqlCompiler.getInstance().create(tableName, columnGenerator -> {
                //创建id
                columnGenerator.id();
                //创建当前执行的迁移
                columnGenerator.string("migration");
                //创建当前执行的第几步
                columnGenerator.integer("batch");
            });
        }
    }

    @Override
    public Column sqlConnectorGetLast() {
        List<Map<String, Object>> last = MySqlConnector.getInstance().nativeQueryList("SELECT * FROM migrations ORDER BY id DESC LIMIT 1", new ArrayList<>()).toMapList();
        if (last.size() > 0) {
            Map<String, Object> map = last.get(0);
            Column column = new Column();
            column.id = String.valueOf(map.get("id"));
            column.migration = String.valueOf(map.get("migration"));
            column.batch = (Integer) map.get("batch");
            return column;
        }
        return null;
    }

    @Override
    public SqlCompiler sqlConnectorGetCompiler() {
        return MySqlCompiler.getInstance();
    }

    @Override
    public void upFinish(String name, int batch) {
        name = name.replace("'", "\\'");
        MySqlConnector.getInstance().runOne("INSERT INTO migrations(migration,batch) VALUES('" + name + "'," + batch + ")");
    }

    @Override
    public List<String> sqlConnectorGetBatchList(int batch) {
        List<String> batchList = new ArrayList<>();
        List<Map<String, Object>> last = MySqlConnector.getInstance().nativeQueryList("SELECT * FROM migrations WHERE batch = '" + batch + "' ORDER BY id DESC", new ArrayList<>()).toMapList();
        for (Map<String, Object> item : last) {
            batchList.add(String.valueOf(item.get("migration")));
        }
        return batchList;
    }

    @Override
    public void downFinish(String name, int batch) {
        name = name.replace("'", "\\'");
        //删除对应那条数据
        MySqlConnector.getInstance().runOne("DELETE FROM migrations WHERE migration = '" + name + "' AND batch = " + batch);
    }

    @Override
    public List<String> sqlConnectorGetExistsList() {
        List<String> batchList = new ArrayList<>();
        List<Map<String, Object>> last = MySqlConnector.getInstance().nativeQueryList("SELECT * FROM migrations ORDER BY id DESC", new ArrayList<>()).toMapList();
        for (Map<String, Object> item : last) {
            batchList.add(String.valueOf(item.get("migration")));
        }
        return batchList;
    }

    @Override
    public void up() {
        super.up();
    }

    @Override
    public void down() {
        super.down();
    }

}
